package com.zh.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class DbUtil {

	public static final String URL = "jdbc:mysql://andSys.com:3306/ssmZH?useUnicode=true&characterEncoding=utf-8";
	public static final String USER = "root";
	public static final String PASSWORD = "123456";

	public static void main(String[] args) throws Exception {

		List<User> list = new ArrayList<User>();

		// 1.加载驱动程序
		Class.forName("com.mysql.jdbc.Driver");
		// 2. 获得数据库连接
		Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);

		// 3.操作数据库，实现增删改查
		Statement stmt = conn.createStatement();
		ResultSet rs = stmt.executeQuery("SELECT id,uname,unumber FROM user_info");
		// 如果有数据，rs.next()返回true
		while (rs.next()) {
			User user = new User();
			user.setId(rs.getInt("id"));
			user.setName(rs.getString("uname"));
			list.add(user);
			System.out.println(rs.getInt("id") + " 名称：" + rs.getString("uname"));
		}

		for (User user : list) {
			System.out.println(user);
		}

	}
}